package com.yequan.hbb.view;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.yequan.hbb.R;
import com.yequan.hbb.app.MyApplication;
import com.yequan.hbb.view.fragment.HBCircleFragment;
import com.yequan.hbb.view.fragment.LearnHBFragment;
import com.yequan.hbb.view.fragment.MineFragment;
import com.yequan.hbb.view.fragment.QuestionFragment;
import com.yequan.hbb.view.fragment.TuantuanFragment;


public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup main_rg;
    private ImageView mainMessage;
    private ImageView mainAdd;
    private ImageView mainGift;
    private Fragment lastFragment;
    private LearnHBFragment learnHBFragment;
    private HBCircleFragment hbCircleFragment;
    private TuantuanFragment tuantuanFragment;
    private QuestionFragment questionFragment;
    private MineFragment mineFragment;
    private long start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Fresco.initialize(this);
        MyApplication.context = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyApplication.main=findViewById(R.id.activity_main);
        main_rg = (RadioGroup) findViewById(R.id.main_rg);
        mainMessage = (ImageView) findViewById(R.id.main_message);
        mainAdd = (ImageView) findViewById(R.id.main_add);
        mainGift = (ImageView) findViewById(R.id.main_gift);
        animation(mainGift);
        if (Build.VERSION.SDK_INT < 24) {
            for (int i = 0; i < main_rg.getChildCount(); i++) {
                View childAt = main_rg.getChildAt(i);
                if (childAt instanceof RadioButton) {
                    RadioButton radioButton = (RadioButton) childAt;
                    Drawable drawable = radioButton.getCompoundDrawables()[1];
                    DrawableCompat.setTintList(drawable, getResources().getColorStateList(R.color.main_rb_text_color));
                }
            }
        }
        main_rg.setOnCheckedChangeListener(this);
        learnHBFragment = LearnHBFragment.getInstance();
        hbCircleFragment = new HBCircleFragment();
        tuantuanFragment = new TuantuanFragment();
        questionFragment = new QuestionFragment();
        mineFragment = new MineFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.main_container, learnHBFragment).commit();
        lastFragment = learnHBFragment;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.main_learn_hb:
                changeFragment(learnHBFragment);
                mainMessage.setVisibility(View.INVISIBLE);
                mainAdd.setVisibility(View.INVISIBLE);
                mainGift.setVisibility(View.VISIBLE);
                break;
            case R.id.main_hb_circle:
                changeFragment(hbCircleFragment);
                mainMessage.setVisibility(View.VISIBLE);
                mainGift.setVisibility(View.GONE);
                mainAdd.setVisibility(View.VISIBLE);
                break;
            case R.id.main_tuantuan:
                changeFragment(tuantuanFragment);
                mainMessage.setVisibility(View.INVISIBLE);
                mainAdd.setVisibility(View.INVISIBLE);
                mainGift.setVisibility(View.INVISIBLE);
                break;
            case R.id.main_question:
                changeFragment(questionFragment);
                mainMessage.setVisibility(View.VISIBLE);
                mainAdd.setVisibility(View.VISIBLE);
                mainGift.setVisibility(View.INVISIBLE);
                break;
            case R.id.main_me:
                if (true) {
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    changeFragment(mineFragment);
                    mainMessage.setVisibility(View.VISIBLE);
                    mainAdd.setVisibility(View.VISIBLE);
                    mainGift.setVisibility(View.INVISIBLE);
                }
                break;
        }
    }

    private void changeFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            if (lastFragment != null) {
                fragmentTransaction.hide(lastFragment);
            }
            fragmentTransaction.add(R.id.main_container, fragment).commit();
        } else {
            fragmentTransaction.hide(lastFragment).show(fragment).commit();
        }
        lastFragment = fragment;
    }

    private void animation(ImageView imageView){
        Animator animator= AnimatorInflater.loadAnimator(this,R.animator.main_gift_animator);
        animator.setTarget(imageView);
        animator.start();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

     /*   if (keyCode==KeyEvent.KEYCODE_BACK) {

            if (System.currentTimeMillis()-start>=2000){
                Toast.makeText(this, "再按一次返回键退出应用", Toast.LENGTH_SHORT).show();
                start = System.currentTimeMillis();
                return true;
            }else{
                finish();
            }
        }

*/
        return super.onKeyDown(keyCode, event);
    }
}
